﻿namespace Pibby.Core.IO
{
	public static class DriveMaskTranslator
	{
		/* Stolen from MSDN */

		/*------------------------------------------------------------------
			FirstDriveFromMask (unitmask)

			Description
				Finds the first valid drive letter from a mask of drive letters.
				The mask must be in the format bit 1 = A, bit 2 = B, bit 3 = C, 
				etc. A valid drive letter is defined when the corresponding bit 
				is set to 1.

			Returns the first drive letter that was found.
		--------------------------------------------------------------------*/
		public static char IdentifyDriveLetter(uint mask)
		{
			char driveLetter = '?';
			const string DriveLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

			if (mask == 0)
			{
				driveLetter = '?';
			}
			else
			{
				foreach (char letter in DriveLetters)
				{
					if ((mask & 0x1) == 0x1)
					{
						driveLetter = letter;
						break;
					}

					mask = mask >> 1;
				}
			}

			return driveLetter;
		}
	}
}
